package com.example.rubbish.Ui;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.example.rubbish.R;

import java.util.Calendar;

public class ReviseDialogView extends LinearLayout {

    Context mContext;
    EditText edit_date;
    EditText edit_garbageName;
    EditText edit_cateName;
    DatePickerDialog dialog;


    public ReviseDialogView(Context context) {
        super(context);
        init(context);
    }

    public ReviseDialogView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ReviseDialogView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        mContext=context;
        LayoutInflater.from(context).inflate(R.layout.revise_dialog,this);
        edit_date=findViewById(R.id.revise_date);
        edit_garbageName=findViewById(R.id.revise_garbageName);
         edit_cateName=findViewById(R.id.revise_cateName);

        //不可粘贴，长按不会弹出粘贴框
        edit_date.setKeyListener(null);


         edit_date.setOnTouchListener(new View.OnTouchListener() {
             @Override
             public boolean onTouch(View view, MotionEvent motionEvent) {
                 if(dialog!= null&& dialog.isShowing()){
                     return  true;
                 }
                 if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    edit_date.clearFocus();
                    edit_date.requestFocus();
                 }
                 if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                     dialog=showDataPickDialog(edit_date);
                     dialog.show();
                 }

                 return false;
             }
         });
    }

     private  DatePickerDialog showDataPickDialog(final  EditText editText){
         int year = 0;
         int month = 0;
         int day = 0;
         //首次打开
         if(editText.getText().toString().length()==0) {
             Calendar c = Calendar.getInstance();
             year = c.get(Calendar.YEAR);
             month = c.get(Calendar.MONTH);
             day = c.get(Calendar.DAY_OF_MONTH);
         }else{//有值的情况下
             String currDate = editText.getText().toString();
             year = Integer.parseInt(currDate.split("-")[0]);
             month = Integer.parseInt(currDate.split("-")[1])-1;
             day = Integer.parseInt(currDate.split("-")[2]);
         }

         final  DatePickerDialog datePickerDialog= new DatePickerDialog(mContext,null,year,month,day);
        datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,"完成",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                DatePicker datePicker = datePickerDialog.getDatePicker();
                int year = datePicker.getYear();
                int month = datePicker.getMonth();
                int day = datePicker.getDayOfMonth();

                StringBuilder monthBuilder = new StringBuilder();
                if (month + 1 < 10) {
                    monthBuilder.append("0");
                }
                monthBuilder.append(month + 1);

                StringBuilder dayBuilder = new StringBuilder();
                if (day < 10) {
                    dayBuilder.append("0");
                }
                dayBuilder.append(day);

                //点击完成将处理好的日期值写到edittext上
                editText.setText(year + "-" + monthBuilder + "-" + dayBuilder);
            }
        });

        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消",
         new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
                 datePickerDialog.cancel();
             }
         });
        datePickerDialog.getDatePicker().setCalendarViewShown(false);
        return datePickerDialog;

    }


}
